package it.unipi.di.rasip;

import javax.microedition.sip.SipAddress;
import javax.microedition.sip.SipDialog;

import it.unipi.di.rasip.utils.RasipConstant;

public class RasipUser {
	private static RasipUser localUser;
	
	String username;
	int protocol;
	String URI;
	
	public static RasipUser getLocalUser() {
		return localUser;
	}
	
	public static void setLocalUser(RasipUser u) {
		localUser = u;
	}
	
	public static RasipUser rasipUserFromURI(String uri) {
		if(uri.startsWith("sip:")) { //Elimino la stringa che identifica il protocollo
			uri = uri.substring(4);
		}
		String remoteUsername = uri.substring(0, uri.indexOf("@"));
		RasipUser u = new RasipUser(remoteUsername, RasipConstant.SIP_PROTOCOL, uri);
		return u;
	}

	public static RasipUser rasipUserFromURI(SipAddress sd) {
		String remoteUsername = sd.getUser();
		RasipUser u = new RasipUser(remoteUsername, RasipConstant.SIP_PROTOCOL, sd.getURI() );
		return u;
	}
	
	public RasipUser(String username, int protocol, String uri) {
		super();
		this.username = username;
		this.protocol = protocol;
		URI = uri;
	}
	
	public int getProtocol() {
		return protocol;
	}
	public void setProtocol(int protocol) {
		this.protocol = protocol;
	}
	public String getURI() {
		return URI;
	}
	public void setURI(String uri) {
		URI = uri;
	}
	public String getCompleteURI() {
		switch (protocol) {
		case RasipConstant.SIP_PROTOCOL:
			return "sip:"+URI;

		case RasipConstant.SKYPE_PROTOCOL:
			return "skype:"+URI; 
		}
		return null;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
}
